var app = getApp();
Page(Object.assign({},{
    data:{
        page : {
            isend : false,
            doing : false,
            doo : 'admin',
            pdata : {
                op : 'orderlist',
                page : 1,
                status : 0,
                type : 0,
                search : '',
                ov : 1,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        tab : {
            color : '#449aff',
            list: [{
            id: '0',
                title: '全部',
            }, {
            id: '1',
                title: '待付款',
            }, {
            id: '2',
                title: '待完成',
            }, {
            id: '3',
                title: '已完成',
            }, {
            id: '4',
                title: '已退款',
            }, {
            id: '5',
                title: '退款中',
            }],
            selectedId: '0',
            scroll: true
        },
        showdeal : false,
        showpstype :false,
        isIPX : app.globalData.isIPX ? 1 : 0,
        psarr : [],
        pstype : '0',
        sendpstype : '0',
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        
        self.setData({
            'page.pdata.type' : options.type ? options.type : 0,
        });

        app.com.auth(function(user){
            app.com.getPage(self,self.data.page,0,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   inited : true,
                   iscomo : data.iscomo,
                   params : data.params,
                   psarr : data.psarr,
                });
                if(data.params.showfail == 0){
                    self.data.tab.list.push({
                    id: '6',
                        title: '已失效',
                    });
                    self.setData({
                        tab : self.data.tab,
                    })
                }
            },false,function(res){
                app.com.alert( res.data.message,function(){
                    app.globalData.adminurl = app.com.getUrlArgs();
                    if( res.data.errno == 2 ){
                        wx.setStorageSync('adminshopid','');
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }
                });
            });
        });
        wx.setNavigationBarTitle({
          title: '订单管理',
        });
        
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '订单管理',
            path: '',
        }
    },
    onReachBottom : function(){
        var self = this;

        app.com.getPage(self,self.data.page,0,function(res){
            if(self.data.datalist.length >= 100){
                self.setData({
                    datalist : res.data.data.list
                });
            }else{
                self.setData({
                    datalist : self.data.datalist.concat(res.data.data.list)
                });
            }
        });
    },
    canceldeal(){
        this.setData({
            showdeal : false,
        })
    },
    dealorder( e ){
        var self = this;
        var m = e.currentTarget.dataset.i;
        var ii = e.currentTarget.dataset.ii;
        var rtype = e.currentTarget.dataset.rtype;
        var id = e.currentTarget.dataset.id;
        var type = e.currentTarget.dataset.type;
        var cartoid = e.currentTarget.dataset.cartoid;
        var pstype = e.currentTarget.dataset.pstype;

        if(cartoid) {
            this.setData({
                id : id,
                type : type,
                cartoid : cartoid,
                showdeal : true,
            })
            return false;
        }

        var postdata = {
            oid : id,
            op:'order'+type,
            pstype : pstype,
        };

        if( type == 'cancel' || type == 'com' || type == 'refund' || type == 'printa' ){
            var nostr = '确定删除订单吗？';
            var comstr = '已删除';
            if( type == 'com' ) {
                var nostr = '确定完成订单吗？';
                var comstr = '已完成';
            }
            if( type == 'refund' ) {
                var nostr = '确定为此订单退款吗？';
                var comstr = '已退款';
            }
            if( type == 'printa' ) {
                var nostr = '确定打印订单吗？';
                var comstr = '已打印';
            }
            app.com.confirm(nostr,function(){
                app.com.http('admin','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        if(type == 'refund'){
                            if(rtype*1 == 1){
                                self.setData({
                                    ['datalist['+m+'].down['+ii+'].isback']: 0
                                });
                            }else{
                                for (var i = 0; i < self.data.datalist[m].down.length; i++) {
                                    self.setData({
                                        ['datalist['+m+'].down['+i+'].isback']: 0
                                    });
                                }
                            }
                            self.setData({
                                ['datalist['+m+'].isback']: 0
                            });
                        }else{
                            for (var i = 0; i < self.data.datalist.length; i++) {
                                if( (!postdata.isall && self.data.datalist[i].orderid == id) || (postdata.isall && self.data.datalist[i].cartoid == self.data.cartoid) ){
                                    self.data.datalist.splice(i,1);
                                    i = i-1;
                                }
                            }
                            self.setData({
                                datalist: self.data.datalist
                            });
                        }
                        app.com.toast(comstr);

                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        }else if( type == 'send' ){
            self.setData({
                expressoid : id,
                showexpress: true,
                isall : postdata.isall,
            });
        }else if( type == 'take' ){
            app.com.http('admin','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    for (var i = 0; i < self.data.datalist.length; i++) {
                        if( (!postdata.isall && self.data.datalist[i].orderid == id) || (postdata.isall && self.data.datalist[i].cartoid == self.data.cartoid) ){
                            self.setData({
                                ['datalist['+i+'].istaked']: 1
                            });
                            app.com.toast('已设为接单');
                        }
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        }else if( type == 'cancelps' ){
            app.com.confirm('确定取消配送吗？',function(){
                app.com.http('admin','POST',postdata,0,true,'','',function(res){
                    if ( !res.data.errno && res.data.errno !== undefined ) {
                        self.setData({
                            ['datalist['+m+'].iscancancelps']: 0
                        });
                        app.com.toast(res.data.message);
                    }else{
                        app.com.alert(res.data.message);
                    }
                });
            });
        }else if( type == 'pubps' ){
            self.setData({
                showpstype : true,
                taroid : id,
                tarm : m,
            })
        }
    },
    pubps(e){
        var self = this;
        var postdata = {
            oid : this.data.taroid,
            op:'orderpubps',
            sendpstype : this.data.sendpstype,
            pstype : this.data.pstype,
        };
        app.com.confirm('确定发起吗？',function(){
            app.com.http('admin','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    self.setData({
                        ['datalist['+self.data.tarm+'].iscanpubps']: 0,
                        showpstype : false,
                    });
                    app.com.toast(res.data.message);
                }else{
                    app.com.alert(res.data.message);
                }
            });
        })
    },
    canceldealps(){
        this.setData({
            showpstype : false,
        })
    },
    selectdeal(e){
        var tar = {
            currentTarget : {
                dataset : {
                    id : this.data.id,
                    type : this.data.type,
                }
            },
            isall : false,
        }
        if(e.detail.type == 1){
            this.dealorder(tar);
        }else{
            tar.isall = true;
            this.dealorder(tar);
        }
    },
    subexpress(){ // 发货
        var self = this;
        var postdata = {
            op : 'send',
            oid : self.data.expressoid,
            name : self.data.expressname ? self.data.expressname : '',
            num : self.data.expressnum ? self.data.expressnum : '',
            isall : self.data.isall,
        };
        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if ( !res.data.errno && res.data.errno !== undefined ) {
                for (var i = 0; i < self.data.datalist.length; i++) {
                    if( (!self.data.isall && self.data.datalist[i].orderid == self.data.expressoid) || (self.data.isall && self.data.datalist[i].cartoid == self.data.cartoid) ){
                        self.data.datalist.splice(i,1);
                        i = i-1;
                    }
                }
                self.setData({
                    datalist: self.data.datalist,
                    showexpress : false,
                });
                app.com.toast(res.data.message);
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    expressname(e){
        this.setData({
            expressname : e.detail.value,
        });
    },
    expressnum(e){
        this.setData({
            expressnum : e.detail.value,
        });
    },
    toggleexpress(){
        this.setData({
            showexpress: false,
        });
    },
    onClickps(e){
        this.setData({
            pstype : e.currentTarget.dataset.name+''
        })
    },
    onClicksend(e){
        this.setData({
            sendpstype : e.currentTarget.dataset.name+''
        })
    },
    togglebuy : function(e){
        this.setData({
            showtaked : 0
        });
    },
    intaked : function(e){
        this.setData({
            takedtext : e.detail.value
        });
    },
    confirmtaked : function(e){
        var self = this;
        var postdata = {
            oid : self.data.showtakedid,
            op:'ordertaked',
            mess : self.data.takedtext
        };
        app.com.confirm('确定提交接单吗？',function(){
            app.com.http('admin','POST',postdata,0,true,'','',function(res){
                if ( !res.data.errno && res.data.errno !== undefined ) {
                    for (var i = 0; i < self.data.datalist.length; i++) {
                        if( self.data.datalist[i].orderid == postdata.oid ){
                            self.setData({
                                ['datalist['+i+'].istaked']: 1,
                                showtaked : 0,
                            });
                            app.com.toast('已设为接单');
                        }
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        })
    },
    tab(e) {
        var self = this;
        var index = e.detail.index;
        if( self.data.ising || index == self.data.page.pdata.status ) return false;
        self.data.ising = true;
        
        self.setData({
            datalist : [],
            'page.pdata.status' : index,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;

    },
    onChanges(e){
        this.data.page.pdata.search = '';
    },
    onSearch(e){
        var self = this;
        self.data.page.pdata.search = e.detail;
        self.setData({
            datalist : [],
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    }
}))